package com.xcity.game.task;

import java.util.Set;

import gnu.trove.map.TByteObjectMap;
import gnu.trove.map.hash.TByteObjectHashMap;
import naga.x.util.ClassUtil;

public class TaskDetectorFactory {

	protected static TByteObjectMap<TaskDetector> detectors = new TByteObjectHashMap<TaskDetector>();
	
	static {
		String pkg = TaskDetectorFactory.class.getPackage().getName() + ".detector";
		Set<Class<?>> classes = ClassUtil.scanPackage(pkg);
		for (Class<?> cls : classes) {
			if (TaskDetector.class.isAssignableFrom(cls)) {
				try {
					TaskDetectorLabel label = cls.getDeclaredAnnotation(TaskDetectorLabel.class);
					TaskDetector detector = (TaskDetector) cls.newInstance();
					detectors.put(label.value(), detector);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public static TaskDetector create(byte type) {
		return detectors.get(type);
	}
	
	public static void register(byte type, TaskDetector detector) {
		detectors.put(type, detector);
	}
	
}
